﻿#include "addtypedialog.h"
#include "ui_addtypedialog.h"
#include <QRadioButton>
#include <QGridLayout>
#include <QDebug>
#include <QSqlQuery>


AddTypeDialog::AddTypeDialog(QString old_id,Dbtools *dbtools, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddTypeDialog)
{
    ui->setupUi(this);
    //窗口关闭时，释放内存
    this->setAttribute(Qt::WA_DeleteOnClose);
    id = old_id;
    if(!old_id.isEmpty()){
        //传入的ID不为空时回显历史数据
        QSqlQuery query(dbtools->getDb());
        query.prepare("select * from mytypes where id=:id");
        query.bindValue(":id",old_id);
        query.exec();
        while(query.next()){
            name = query.value("type_name").toString();
            icon = query.value("icon").toString();
        }
        this->setWindowTitle("修改分类");
    }else{
        this->setWindowTitle("新增分类");
    }

    ui->type_lineEdit->setText(name);
    initAllIcon();
}

AddTypeDialog::~AddTypeDialog()
{
    delete ui;
}

void AddTypeDialog::on_ok_pushButton_clicked()
{
    QObjectList list =  ui->icon_widget->children();
    QString iconStr;
    foreach (QObject *obj, list) {
        if(obj->metaObject()->className()==QStringLiteral("QRadioButton")){
            QRadioButton *rb = static_cast<QRadioButton*>(obj);
            if(rb->isChecked()){
                iconStr.append(rb->windowRole());
                break;
            }
        }
    }

    emit saveNewType(ui->type_lineEdit->text(),id,iconStr);

    close();
}

void AddTypeDialog::initAllIcon()
{
    QStringList iconStr;
    iconStr.append(":/new/prefix1/res/accept.png");
    iconStr.append(":/new/prefix1/res/add.png");
    iconStr.append(":/new/prefix1/res/application_go.png");
    iconStr.append(":/new/prefix1/res/application_view_list.png");
    iconStr.append(":/new/prefix1/res/book.png");
    iconStr.append(":/new/prefix1/res/cog.png");
    iconStr.append(":/new/prefix1/res/cog_edit.png");
    iconStr.append(":/new/prefix1/res/connect.png");
    iconStr.append(":/new/prefix1/res/control_rewind.png");
    iconStr.append(":/new/prefix1/res/cut.png");
    iconStr.append(":/new/prefix1/res/delete.png");
    iconStr.append(":/new/prefix1/res/error.png");
    iconStr.append(":/new/prefix1/res/feed_add.png");
    iconStr.append(":/new/prefix1/res/feed_delete.png");
    iconStr.append(":/new/prefix1/res/folder_go.png");
    iconStr.append(":/new/prefix1/res/folder_wrench.png");
    iconStr.append(":/new/prefix1/res/hmenu-lock.png");
    iconStr.append(":/new/prefix1/res/hmenu-unlock.png");
    iconStr.append(":/new/prefix1/res/image_add.png");
    iconStr.append(":/new/prefix1/res/information.png");
    iconStr.append(":/new/prefix1/res/page_copy.png");
    iconStr.append(":/new/prefix1/res/page_white_edit.png");
    iconStr.append(":/new/prefix1/res/paintbrush.png");
    iconStr.append(":/new/prefix1/res/paste_plain.png");
    iconStr.append(":/new/prefix1/res/rss_go.png");
    iconStr.append(":/new/prefix1/res/table_refresh.png");
    iconStr.append(":/new/prefix1/res/text_list_bullets.png");
    iconStr.append(":/new/prefix1/res/user.png");


    QGridLayout *layout = new QGridLayout(ui->icon_widget);
    //layout->rowStretch()
    int rownum=0;
    int colnum=0;

    for(int x=0;x<iconStr.count();x++){
        QRadioButton *rb = new QRadioButton(this);
        rb->setIcon(QIcon(iconStr.at(x)));
        rb->setWindowRole(iconStr.at(x));
        if(!id.isEmpty()&&QString(iconStr.at(x))==icon){
            rb->setChecked(true);
        }

        layout->addWidget(rb,rownum,colnum);
        if(x>0 && (x+1)%7==0){

            rownum++;
            colnum=0;
        }else{
            colnum++;
        }
    }

}

void AddTypeDialog::on_cancel_pushButton_clicked()
{

}
